Check crate name for invalid characters in cargo new
authorAku Kotkavuo <aku.kotkavuo@gmail.com>
Mon, 13 Oct 2014 00:05:41 +0000 (03:05 +0300)
committerAku Kotkavuo <aku.kotkavuo@gmail.com>
Mon, 13 Oct 2014 23:37:25 +0000 (02:37 +0300)
src/cargo/ops/cargo_new.rs
tests/test_cargo_new.rs

index 79191ecaa1f93f49d356b3f4ddf2f0e0f6c78255..856603bfec8c52d5588e0f00daad5b2bbd71f9ac 100644 (file)
@@ -29,6 +29,12 @@ pub fn new(opts: NewOptions, _shell: &mut MultiShell) -> CargoResult<()> {
                                  path.display())))
     }
     let name = path.filename_str().unwrap();
+    for c in name.chars() {
+        if c.is_alphanumeric() { continue }
+        if c == '_' || c == '-' { continue }
+        return Err(human(format!("Invalid character `{}` in crate name: `{}`",
+                                 c, name).as_slice()));
+    }
     mk(&path, name, &opts).chain_error(|| {
         human(format!("Failed to create project `{}` at `{}`",
                       name, path.display()))
index 58b5c99a91bdb35e375ae7d3c96598993d98b691..cc218483843ba11234ada7a2d4e65189213bcd80 100644 (file)
@@ -100,6 +100,12 @@ test!(existing {
                                             dst.display())));
 })
 
+test!(invalid_characters {
+    assert_that(cargo_process("new").arg("foo.rs"),
+                execs().with_status(101)
+                       .with_stderr("Invalid character `.` in crate name: `foo.rs`"));
+})
+
 test!(finds_author_user {
     // Use a temp dir to make sure we don't pick up .cargo/config somewhere in
     // the hierarchy